import { getUserCouponList } from '../../services/user'
import { dateFormat } from '../../utils/date'
import { priceToYuan } from '../../utils/utils'

Page({
  data: {
    tabs: [
      {
        id: 0,
        title: '全部',
        onClick: () => { },
      },
      {
        id: 1,
        title: '已领取',
        onClick: () => { },
      },
      {
        id: 2,
        title: '已使用',
        onClick: () => { },
      },
      {
        id: 3,
        title: '已过期',
        onClick: () => { },
      },
    ],
    tickets: [],
  },
  onLoad() {
    my.setNavigationBar({
      title: '我的优惠券',
      backgroundColor: '#4BA3F9'
    })

    this.getUserCouponList()
  },
  getUserCouponList() {
    getUserCouponList({}).then(res => {
      if (res.code == 0) {
        this.setData({ tickets: res.data.list.map((item, index) => ({
          id: item.id,
          couponAmount: priceToYuan(item.discount),
          minPurchase: priceToYuan(item.threshold),
          couponStart: dateFormat(item.start_date),
          couponEnd: dateFormat(item.end_date),
          couponName: item.name,
          action: 0,
        }))})
      }
    })
  },
  onTabItemTap(id) {
    console.log(id);
    this.setData({ activeId: id });
  },
});
